package com.tmt.paymentcenter.web.configure.ignite;

import org.apache.ignite.Ignite;
import org.apache.ignite.Ignition;
import org.apache.ignite.cache.spring.SpringCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.URL;

/**
 * Created by ThreeManTeam on 2017/9/11.
 */
@Configuration
@ConditionalOnProperty(name={"ignite.enabled"}, havingValue = "true", matchIfMissing = true)
@EnableConfigurationProperties(IgniteProperties.class)
public class IgniteConfig {

    @Autowired
    private IgniteProperties igniteProperties;

    @Bean
    public Ignite igniteInstance() {
        URL fileURL=this.getClass().getResource(igniteProperties.getConfig());
        Ignite ignite = Ignition.start(fileURL);
        return ignite;
    }

    @Bean
    public CacheManager cacheManager(@Autowired Ignite igniteInstance) {
        SpringCacheManager cm = new SpringCacheManager();
        cm.setIgniteInstanceName(igniteInstance.name());
        return cm;
    }

}
